package leetcodev1.字符串;

import java.util.Arrays;
import java.util.Stack;

public class LeetCode71 {

    public static void main(String[] args) {
        String[] elements = "/home/".split("/");
        System.out.println(Arrays.toString(elements));
    }

    public String simplifyPath(String path) {
        String[] elements = path.split("/");
        Stack<String> stack = new Stack<>();
        for (String element : elements) {
            if (element.equals(".") || element.equals("")) {
                continue;
            }

            if (element.equals("..")) {
                if (!stack.empty()){
                    stack.pop();
                }
                continue;
            }

            stack.push(element);
        }
        StringBuilder ret = new StringBuilder();
        for (String str : stack) {
            ret.append("/").append(str);
        }
        return ret.length() == 0 ? "/" : ret.toString();
    }
}
